﻿using System;
using System.Windows.Forms;

namespace Kay.Server.Demo
{
    public partial class StartUp : Form
    {
        public StartUp()
        {
            InitializeComponent();
        }

        private TcpInterNetworkStreamServer _streamServer;

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            _streamServer.Stop();
        }

        private void toolStripMenuItem_Start_Click(object sender, EventArgs e)
        {
            toolStripTextBox_Port.ReadOnly = true;
            _streamServer.Start();
        }

        private void toolStripMenuItem_Stop_Click(object sender, EventArgs e)
        {
            _streamServer.Stop();
            toolStripTextBox_Port.ReadOnly = false;
            dataSet.UsersTable.Rows.Clear();
        }

        private void StartUp_Load(object sender, EventArgs e)
        {
            try
            {
                var port = Convert.ToUInt16(toolStripTextBox_Port.Text);
                _streamServer = new TcpInterNetworkStreamServer(port);
            }
            catch
            {
                _streamServer = new TcpInterNetworkStreamServer(5555);
            }
            _streamServer.UserOnline += OnSocketServerUserOnline;
            _streamServer.UserOffline += OnSocketServerUserOffline;
            _streamServer.IsRuningChanged += OnStreamServerIsRuningChanged;
            _streamServer.Start();
        }

        private void OnStreamServerIsRuningChanged(bool obj)
        {
            if (menuStrip.InvokeRequired)
            {
                menuStrip.Invoke(new Action<bool>(OnStreamServerIsRuningChanged), obj);
                return;
            }
            toolStripMenuItem_Start.Enabled = !obj;
            toolStripMenuItem_Stop.Enabled = obj;
            try
            {
                toolStripTextBox_Port.ReadOnly = obj;
            }
            catch
            {
                // ignored
            }
        }

        private void OnSocketServerUserOffline(object sender, ContactsInfoEventArgs e)
        {
            if (dataGridView1.InvokeRequired)
            {
                dataGridView1.Invoke(new Action<object, ContactsInfoEventArgs>(OnSocketServerUserOffline), sender, e);
                return;
            }

            var row = dataSet.UsersTable.Rows.Find(e.UserInfo.Id);
            if (row != null)
                dataSet.UsersTable.Rows.Remove(row);
        }

        private void OnSocketServerUserOnline(object sender, ContactsInfoEventArgs e)
        {
            if (dataGridView1.InvokeRequired)
            {
                dataGridView1.Invoke(new Action<object, ContactsInfoEventArgs>(OnSocketServerUserOnline), sender, e);
                return;
            }

            var userInfo = _streamServer.Users.Find(n => n.Id == e.UserInfo.Id);
            if (userInfo == null) return;
            var row = dataSet.UsersTable.NewUsersTableRow();
            row.Id = userInfo.Id;
            row.Name = userInfo.Name;
            row.Time = userInfo.LoginTime;
            row.Ip = userInfo.RemoteEndPointByServer;
            row.Ip2 = userInfo.LocalEndPointByServer;
            row.Ip3 = userInfo.LocalEndPointByClient;
            row.Ip4 = userInfo.RemoteEndPointByClient;
            row.WebSocket = userInfo.WebSocket;
            dataSet.UsersTable.Rows.Add(row);
        }

        private void toolStripMenuItem_Close_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                return;
            }
            var rowId = dataGridView1.SelectedRows[0].Cells[Column_Id.Name].Value.ToString();
            _streamServer.CloseClient(rowId);
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1 && !dataGridView1.Rows[e.RowIndex].Selected)
            {
                dataGridView1.Rows[e.RowIndex].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
        }
    }
}
